package controllers;

	import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

import model.ComplaintModel;
import model.UserPermissions;
import view.NotificationDetailsGUI;
import client.CliMessage;


		/**
		 * This Class is a Notification Details Controller which  extends AbstractController.
		 * @author Tal
		 *
		 */

		public class NotificationDetailsController extends AbstractController
		{	
					private NotificationDetailsGUI NotDetailsView;
					private ComplaintModel ComplaintModel;
					
					/**
					 * This is ComplaintDetailsController controller constructor.
					 * @param NotDetailsView is a Notification Details Window instance.
					 * @param ComplaintModel is a Complaint Model instance.
					 */
					public NotificationDetailsController(NotificationDetailsGUI NotDetailsView, ComplaintModel ComplaintModel ) 
					{
						super(NotDetailsView,ComplaintModel);
						this.NotDetailsView = NotDetailsView;
						this.ComplaintModel = ComplaintModel;
													
					}
					
					/**
					 * This is setButtons method-this method add listeners to buttons in Notification Details window -reply/Compensation.
					 * 
					 */
					
					public void setButtons(ComplaintModel ComplaintModel)
					{
						
						NotDetailsView.addcheckActionListener(new CheckListener(ComplaintModel));
						
					}
					
				
					/**
					 * Inner class that handles when Check Button Pressed, implements Action listener
					 * @author Tal
					 *
					 */
					
					class CheckListener implements ActionListener
					{
						
						private ComplaintModel CompModel;
						private UserPermissions up;
						public CheckListener(ComplaintModel CompModel)
						{
							this.CompModel=CompModel;
							this.up = UserPermissions.getInstance(0, null,null);
						}
						
						@Override
						public void actionPerformed(ActionEvent arg0) 
						{
			
						   
	                       int comid=CompModel.getComID();
	                   
							//change status in db to handled-2
	                       
							String updateQuary = "adu" + "UPDATE customer_complaints " +
			    					             " SET status = 2 "
			    					             + " WHERE comid = " + comid;
							
						
							
							int curruid=up.getCurrUserID();
							JOptionPane.showMessageDialog(null, "Notification Check !!!" , "Notification Information", JOptionPane.INFORMATION_MESSAGE);
							//get curr notification request
							String searchQuery = "search"	+"SELECT cusComp.comid,cusComp.uid,u.fname,u.lname "
									+" FROM customer_complaints AS cusComp, user AS u"
	                                +" WHERE cusComp.uid=u.uid AND u.uid="+ curruid +" AND cusComp.status=1 ";
	
	                             
							
							//activate the back button
							NotDetailsView.clickBackButton();
							
						       sendToServer(updateQuary);
							   sendToServer(searchQuery);
	                          	
						}
					}

				
					@Override
					public void handleDBRequestSuccess()
					{
						NotDetailsView.displayWrnMessage("Notification Details:");
						
					}
					
					@Override
					public void handleDBSearchResult(CliMessage dbResult) 
					{	
						//get a list of all unhendled complaints
						ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
						String fname = null;
						String lname = null;
						int rows=srs.size();
						
						
						for(int i = 0 ; i <rows  ; i++)
						{
						   Map<String, String> complaintTupple  = srs.get(i);	
							    	
						   System.out.println("com id:"+ComplaintModel.getComID()+""+"user id:"+ComplaintModel.getCID()+" "+"fname:"+complaintTupple.get("FNAME")+" "+"lname:"+complaintTupple.get("LNAME")+""+"details:"+complaintTupple.get("DETAILS")+" "+"reply:"+complaintTupple.get("REPLY")+""+"status"+complaintTupple.get("STATUS"));
						 //initialzie Complaint
						  ComplaintModel.setDetails(complaintTupple.get("DETAILS"));
						  ComplaintModel.setReply(complaintTupple.get("REPLY"));
						  ComplaintModel.setStatus(Integer.parseInt(complaintTupple.get("STATUS")));		
						  //initialize l/fname
						  fname=complaintTupple.get("FNAME");
						  lname=complaintTupple.get("LNAME");
						}	
						
						
						///set fields GUi
						NotDetailsView.setComplaintDetArea(ComplaintModel.getDetails());
						NotDetailsView.setReplyArea(ComplaintModel.getReply());
						
						setButtons(ComplaintModel);
						//display main window
						NotDetailsView.setVisible(true);						
					}

					@Override
					public void handleNullSearchResult() 
					{
						NotDetailsView.displayWrnMessage("Notification Not Found!!!");
					}
	

}
